{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "precise-crash",
   "metadata": {},
   "source": [
    "# Mexican Tax Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "general-treatment",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "impressive-incidence",
   "metadata": {},
   "source": [
    "The function `clean_mx_rfc()` cleans a column containing Mexican tax number (RFC) strings, and standardizes them in a given format. The function `validate_mx_rfc()` validates either a single RFC strings, a column of RFC strings or a DataFrame of RFC strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "direct-enclosure",
   "metadata": {},
   "source": [
    "RFC strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"GODE561231GR8\"\n",
    "* `standard`: RFC strings with proper whitespace in the proper places, like \"GODE 561231 GR8\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_mx_rfc()` and `validate_mx_rfc()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efficient-auditor",
   "metadata": {},
   "source": [
    "### An example dataset containing RFC strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "baking-princess",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"rfc\": [\n",
    "            \"GODE561231GR8\",\n",
    "            \"BUEI591231GH9\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "played-version",
   "metadata": {},
   "source": [
    "## 1. Default `clean_mx_rfc`\n",
    "\n",
    "By default, `clean_mx_rfc` will clean rfc strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "normal-wages",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_mx_rfc\n",
    "clean_mx_rfc(df, column = \"rfc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "living-uncle",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sorted-martial",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "celtic-objective",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "certain-impossible",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mx_rfc(df, column = \"rfc\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "imperial-stable",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ordered-despite",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mx_rfc(df, column = \"rfc\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exterior-overall",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned RFC strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "indoor-incentive",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mx_rfc(df, column=\"rfc\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "religious-beverage",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "rapid-rental",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "exact-pitch",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mx_rfc(df, \"rfc\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reported-relationship",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "single-belarus",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mx_rfc(df, \"rfc\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "circular-contemporary",
   "metadata": {},
   "source": [
    "## 4. `validate_mx_rfc()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "elementary-decade",
   "metadata": {},
   "source": [
    "`validate_mx_rfc()` returns `True` when the input is a valid RFC. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_mx_rfc()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_mx_rfc()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_mx_rfc()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "experimental-judgment",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_mx_rfc\n",
    "print(validate_mx_rfc(\"GODE561231GR8\"))\n",
    "print(validate_mx_rfc(\"BUEI591231GH9\"))\n",
    "print(validate_mx_rfc(\"51824753556\"))\n",
    "print(validate_mx_rfc(\"51 824 753 556\"))\n",
    "print(validate_mx_rfc(\"hello\"))\n",
    "print(validate_mx_rfc(np.nan))\n",
    "print(validate_mx_rfc(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "infinite-strain",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "completed-intersection",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_mx_rfc(df[\"rfc\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "constitutional-sequence",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "disabled-trigger",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_mx_rfc(df, column=\"rfc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "stupid-tokyo",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "likely-hearts",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_mx_rfc(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "postal-twist",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
